"""
Python App Engine + Google Storage.
Based heavily from examples given by Google
Instructions:
just rip the boto lib from the gsutil project, drop it in your python 
App Engine project. 
@author: Adam Thurlow <Thurloat>
"""
"""
import boto
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util, template
config = boto.config
config.add_section('Credentials')
config.set('Credentials', 'gs_access_key_id', 'GOOGWW5JAPTOABAORPVO')
config.set('Credentials', 'gs_secret_access_key', 'ir/YNMlWpHtwGIN4Sf5aJGk5k+P5uAl7D0PfX4Pk')
FLAGS = ['CREATE', 'LIST_BUCKETS', 'LIST_CONTENTS','DOWNLOAD_FILE']
DOWNLOAD_PATH = '/path/to/file.ext'
DOWNLOAD_FILENAME = 'file.ext'
BUCKET_NAME = 'blogassets'
class DownloadHandler(webapp.RequestHandler):
  def get(self):
    srow = self.response.out.write
    if 'DOWNLOAD_FILE' in FLAGS:
      uri = boto.storage_uri("%s/%s" % (BUCKET_NAME, DOWNLOAD_PATH), 'gs')
      self.response.headers['Content-Type'] = 'application/octet-stream'
      self.response.headers.add_header('content-disposition',
        'attachment',
        filename = DOWNLOAD_FILENAME)
      srow(uri.get_contents_as_string())
class MainHandler(webapp.RequestHandler):
  def get(self):
    srow = self.response.out.write
    srow("<h4> Testing the Google Storage API </h4>")
    if 'CREATE' in FLAGS:
      srow("<h2>Creating Bucket</h2>")
      uri = boto.storage_uri(BUCKET_NAME, 'gs')
      uri.create_bucket()
    if 'LIST_BUCKETS' in FLAGS:
      srow("<h2>Listing Buckets</h2>")
      uri = boto.storage_uri('', 'gs')
      buckets = uri.get_all_buckets();
      for bucket in buckets:
        self.response.out.write(bucket.name + '<br />')
    if 'LIST_CONTENTS' in FLAGS:
      srow("<h2>Listing Contents of %s bucket</h2>" % BUCKET_NAME)
      uri = boto.storage_uri(BUCKET_NAME, 'gs')
      objs = uri.get_bucket()
      for obj in objs:
        srow("gs://%s/%s <br />" % (BUCKET_NAME,obj.name))
_URLS = [
  ('/storage', MainHandler),
  ('/storage/', MainHandler),
  ('/storage/download',DownloadHandler),
]


def main():
  application = webapp.WSGIApplication(_URLS,
                                       debug=True)
  util.run_wsgi_app(application)
"""
import os
import boto
config = boto.config
config.add_section('Credentials')
config.set('Credentials', 'gs_access_key_id', 'GOOGWW5JAPTOABAORPVO')
config.set('Credentials', 'gs_secret_access_key', 'ir/YNMlWpHtwGIN4Sf5aJGk5k+P5uAl7D0PfX4Pk')

from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import util, template
from boto.exception import S3ResponseError

GSKEY = "GOOGWW5JAPTOABAORPVO"
GSSECRET = "ir/YNMlWpHtwGIN4Sf5aJGk5k+P5uAl7D0PfX4Pk"

def testConn ():
  conn = httplib.HTTPConnection("commondatastorage.googleapis.com")
  
  conn.request("GET", "/")
  res = conn.getresponse()
    
  result = {
    "status" : res.status,
    "reason" : res.reason,
    "version" : res.version,
    "resbody" : res.read(),
    "Content-Type" : res.getheader("Content-Type")
  }
  
  return result

class MainHandler(webapp.RequestHandler):

  def get(self):
    uri = boto.storage_uri('', 'gs')
    buckets = uri.get_all_buckets();
    for bucket in buckets:
      
      self.response.out.write("<a href=\"/storage/list?bucket=" + bucket.name + "\">" + bucket.name + "</a><br/>")

class ListHandler(webapp.RequestHandler):

  def get(self):
    bucket = self.request.get('bucket')
    uri = boto.storage_uri(bucket, 'gs')
    objs = uri.get_bucket()
    for obj in objs:
      try:
        self.response.out.write("<a target=\"_blank\" href=\"http://%s.commondatastorage.googleapis.com/%s\">%s</a> <br />" % (bucket,obj.name,obj.name))
      except UnicodeDecodeError:
        self.response.out.write("code err<br/>")

def main():
  application = webapp.WSGIApplication([('/storage', MainHandler),
                                    ('/storage/', MainHandler),
                                    ('/storage/list', ListHandler)
                                    ],
                                       debug=True)
  util.run_wsgi_app(application)
